<?php

class ProjectsController extends Zend_Controller_Action {

   public function init() {
   }

   public function createAction() {
      $form = new Application_Form_Project();
      $form->setDecorators(array('FormElements', 'Form'));
      $this->view->form = $form;

      if ($this->getRequest()->isPost()) {
         $formData = $this->getRequest()->getPost();
         if ($form->isValid($formData)) {
            $name = $form->getValue('name');
            $description = $form->getValue('description');
            $projects = new Application_Model_DbTable_Projects();
            $projects->createProject($name, $description);

            $this->_helper->flashMessenger->addMessage(
                    array('success' => 'Project successfully created.'));

            $this->_helper->redirector('index', 'index', null, array());
         } else {
            $form->populate($formData);
         }
      }
   }

   public function showAction() {
      $id = $this->_getParam('id', 0);
      $term = $this->_getParam('term', null);
      $only_status = $this->_getParam('status', null);
      $projects = new Application_Model_DbTable_Projects();
      $this->view->project = $projects->readProject($id);

      $task = new Application_Model_DbTable_Tasks();

      $select = $task->select();
      $select->setIntegrityCheck(false); // TODO: ver o que faz essa função
      $select->from('tasks');
      $select->join('status', 'tasks.status_id = status.id',
              array('st_description' => 'description',
                  'st_css_badge' => 'css_badge'));
      $select->join(array('p' => 'priorities'), 'tasks.priority_id = p.id',
              array('pr_description' => 'description',
                    'pr_cardinal' => 'cardinal',
                    'pr_css_badge' => 'css_badge'));
      $select->where('project_id = ' . $id);
      
      if (! empty($term)) {
         $select->where('tasks.description like ?', '%' . $term . '%');
         $this->view->term = $term;
      }
      
      if (!empty($only_status) && $only_status != 'all') {
         $select->where('status.id = ?', $only_status);
         $this->view->only_status = $only_status;
      }
      
      $select->order(array('status.cardinal ASC',
          'p.cardinal DESC',
          'julianday(tasks.deadline) ASC',
          'julianday(tasks.updated_at) DESC',
          'julianday(tasks.created_at) DESC',
          'tasks.id DESC'
      ));
      $select->limit(100);

      $tasks = $task->fetchAll($select);

      // tasks pagination
      $page = $this->_getParam('page', 1);
      $countPerPage = $this->_getParam('count', 10);

      $paginator = Zend_Paginator::factory($tasks);
      $paginator->setCurrentPageNumber($page)
              ->setItemCountPerPage($countPerPage);

      Zend_Paginator::setDefaultScrollingStyle();
      Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
      $this->view->assign('paginator', $paginator);
      
      $statusModel = new Application_Model_DbTable_Status();
      $resultSet = $statusModel->fetchAll(null, 'cardinal ASC');
      $this->view->status_list = $resultSet;
   }

   public function deleteAction() {
      if ($this->getRequest()->isPost()) {
         $del = $this->getRequest()->getPost('del');
         if ($del == "Ok") {
            $id = $this->getRequest()->getPost('id');
            $projects = new Application_Model_DbTable_Projects();
            $rowset = $projects->find($id);
            $project = $rowset->current();

            // delete tasks cascade ...
            $project->delete();
            $this->_helper->flashMessenger->addMessage(
                    array('success' => 'Project successfully deleted.'));
         } else if ($del == "Hide") {
            $id = $this->getRequest()->getPost('id');
            $projects = new Application_Model_DbTable_Projects();

            $projects->hideProject($id);
            $this->_helper->flashMessenger->addMessage(
                    array('info' => 'Project is now hidden.'));
         }
         $this->_helper->redirector('index', 'index', null, array());
      } else {
         $id = $this->_getParam('id', 0);
         $projects = new Application_Model_DbTable_Projects();
         $this->view->project = $projects->readProject($id);
      }
   }

   public function updateAction() {
      $form = new Application_Form_Project();
      $this->view->form = $form;

      if ($this->getRequest()->isPost()) {
         $formData = $this->getRequest()->getPost();
         if ($form->isValid($formData)) {
            $id = (int) $form->getValue('id');
            $name = $form->getValue('name');
            $description = $form->getValue('description');
            $hide = $form->getValue('hide');
            $projects = new Application_Model_DbTable_Projects();
            $projects->updateProject($id, $name, $description, $hide);

            $this->_helper->flashMessenger->addMessage(
                    array('success' => 'Project successfully updated.'));

            $this->_helper->redirector('index', 'index', null, array());
         } else {
            $form->populate($formData);
         }
      } else {
         $id = $this->_getParam('id', 0);
         if ($id > 0) {
            $projects = new Application_Model_DbTable_Projects();
            $form->populate($projects->readProject($id));
         }
      }
   }
}

